package zero.tech.games.gate.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import zero.tech.core.data.PageBean;
import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.core.util.DatetimeUtil;
import zero.tech.games.api.RemoteExtension;
import zero.tech.games.common.ClubMemberUtil;
import zero.tech.games.common.ClubRoleId;
import zero.tech.games.common.Code;
import zero.tech.games.common.FrozenUtil;
import zero.tech.games.common.InviteInfo;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.common.topic.ProxyAdd;
import zero.tech.games.common.topic.ProxyRebind;
import zero.tech.games.entity.games.CfGame;
import zero.tech.games.entity.games.Club;
import zero.tech.games.entity.games.ClubGame;
import zero.tech.games.entity.games.ClubGameRule;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.entity.games.ClubMemberPermission;
import zero.tech.games.entity.games.ClubMemberProxy;
import zero.tech.games.entity.games.ClubMemberRole;
import zero.tech.games.entity.log.LogClubEvent;
import zero.tech.games.entity.log.LogGoldChange;
import zero.tech.games.entity.log.LogTrade;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.gate.data.GATECMD;
import zero.tech.games.gate.data.consts.LogClubEventType;
import zero.tech.games.gate.data.entity.ReqPermission;
import zero.tech.games.gate.data.entity.ZsInfo;
import zero.tech.games.gate.data.res.ResSelectGameRules;
import zero.tech.games.gate.mapper.ClubGameMapper;
import zero.tech.games.gate.mapper.ClubGameRuleMapper;
import zero.tech.games.gate.mapper.ClubMapper;
import zero.tech.games.gate.mapper.ClubMemberMapper;
import zero.tech.games.gate.mapper.ClubMemberPermissionMapper;
import zero.tech.games.gate.mapper.ClubMemberProxyMapper;
import zero.tech.games.gate.mapper.ClubMemberRoleMapper;
import zero.tech.games.gate.mapper.ClubMemberWaterpakMapper;
import zero.tech.games.gate.service.api.ITradeService;
import zero.tech.games.gate.util.PackageUtil;
import zero.tech.redis.cache.RedisUtil;
import zero.tech.redis.queue.QueueUtil;
import zero.tech.wsnet.data.Message;
@Component
public class ClubUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private ClubMemberUtil clubMemberUtil;
	@Autowired
	private ClubGameMapper clubGameMapper;
	@Autowired
	private ClubGameRuleMapper clubGameRuleMapper;
	@Autowired
	private ClubMemberMapper clubMemberMapper;
	@Autowired
	private ClubMemberProxyMapper clubMemberProxyMapper;
	@Autowired
	private ClubMemberRoleMapper clubMemberRoleMapper;
	@Autowired
	private ClubMemberPermissionMapper clubMemberPermissionMapper;
	@Autowired
	private ClubMemberWaterpakMapper clubMemberWaterpakMapper;
	@Autowired
	private QueueUtil queueUtil;
	@Autowired
	private ClubMapper clubMapper;
	@Autowired
	private ITradeService tradeService;
	@Autowired
	private RemoteExtension remoteExtension;
	@Autowired
	private NotifyUtil notifyUtil;
	@Autowired
	private FrozenUtil frozenUtil;
	public List<Club> loadHotClubList(int uid) {
		List<Club> list=new ArrayList<Club>();
		List<Club> tmp=clubMapper.loadClubListByUid(uid);
		list.addAll(tmp);
		return list;
	}
	public List<CfGame> loadHotClubGames(int club_id) {
		List<CfGame> list=new ArrayList<>();
		String str=(String) redisUtil.get(RedisConsts.HOT_CLUB_GAMES+String.valueOf(club_id));
		if(StringUtils.isBlank(str)) {
			List<CfGame> tmp=clubGameMapper.loadClubGames(club_id);
			list.addAll(tmp);
			redisUtil.set(RedisConsts.HOT_CLUB_GAMES+String.valueOf(club_id),JSONObject.toJSONString(list), 1*60);
		}else {
			List<CfGame> tmp=JSONObject.parseObject(str,new TypeReference<List<CfGame>>() {});
			list.addAll(tmp);
		}
		return list;
	}
	public Club loadHotClub(int club_id) {
		Club club=null;
		String str=(String) redisUtil.get(RedisConsts.HOT_CLUB+String.valueOf(club_id));
		if(StringUtils.isBlank(str)) {
			club=clubMapper.getClub(club_id);
			if(club!=null) {
				redisUtil.set(RedisConsts.HOT_CLUB+String.valueOf(club_id),JSONObject.toJSONString(club), 1*60);
			}
		}else {
			club=JSONObject.parseObject(str, Club.class);
		}
		return club;
	}
	
	public ClubMember getClubMember(int uid, int club_id) {
		String str=(String)redisUtil.hget(RedisConsts.A_CLUBMEMBER+"_"+club_id,String.valueOf(uid));
		ClubMember clubMember=JSONObject.parseObject(str, ClubMember.class);
		return clubMember;
	}
	public void createClub(SimpleUser simpleUser, String club_name) {
		int uid=simpleUser.getUid();
		Club club=new Club();
		club.setClub_name(club_name);
		club.setClub_creater_uid(simpleUser.getUid());
		club.setClub_creater_nick(simpleUser.getNick());
		club.setClub_creater_head_img(simpleUser.getHead_img());
		clubMapper.insert(club);
		int club_id=club.getClub_id();
		ClubMember clubMember=new ClubMember();
		clubMember.setClub_id(club_id);
		clubMember.setSafety(1000000L);
		clubMember.setTea(1000000L);
		clubMember.setFrozen(0L);
		clubMember.setUid(uid);
		clubMemberMapper.insert(clubMember);
		ClubMemberProxy clubMemberProxy=new ClubMemberProxy();
		clubMemberProxy.setClub_id(club_id);
		clubMemberProxy.setUid(uid);
		clubMemberProxy.setFanyong(1f);
		clubMemberProxyMapper.insert(clubMemberProxy);
		ClubMemberPermission clubMemberPermission = new ClubMemberPermission();
		clubMemberPermission.setAdd_person(1);
		clubMemberPermission.setFrozen_line(1);
		clubMemberPermission.setFrozen_person(1);
		clubMemberPermission.setFrozen_person_gold(1);
		clubMemberPermission.setKick_club(1);
		clubMemberPermission.setAdd_money(1);
		clubMemberPermission.setReduce_money(1);
		clubMemberPermission.setSet_hehuoren(1);
		clubMemberPermission.setDel_hehuoren(1);
		clubMemberPermission.setKick_room(1);
		clubMemberPermission.setCreate_game_rule(1);
		clubMemberPermission.setSwitch_game_rule(1);
		clubMemberPermission.setLook_allsubs(1);
		clubMemberPermissionMapper.insert(clubMemberPermission);
		ClubMemberRole clubMemberRole = new ClubMemberRole();
		clubMemberRole.setClub_id(club_id);
		clubMemberRole.setUid(uid);
		clubMemberRole.setRole(ClubRoleId.GUAN_ZHU);
		clubMemberRole.setPermission_id(clubMemberPermission.getPermission_id());
		clubMemberRoleMapper.insert(clubMemberRole);
		redisUtil.hset(RedisConsts.A_CLUBMEMBER+"_"+club_id,String.valueOf(uid), JSONObject.toJSONString(clubMember));
		//添加俱乐部创建日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.CREATE_CLUB);
		logClubEvent.setEvent_msg("用户:"+club.getClub_creater_uid()+"创建了俱乐部!");
		logClubEvent.setEvent_creater(club.getClub_creater_uid());
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void insertClubMember(int father_uid, int uid, int club_id, SimpleUser simpleUser, TUser user, Club club) {
		ClubMember clubMember=new ClubMember();
		clubMember.setClub_id(club_id);
		clubMember.setSafety(1000000L);
		clubMember.setTea(1000000L);
		clubMember.setFrozen(0L);
		clubMember.setUid(uid);
		clubMemberMapper.insert(clubMember);
		ClubMemberProxy clubMemberProxy=new ClubMemberProxy();
		clubMemberProxy.setClub_id(club_id);
		clubMemberProxy.setUid(uid);
		clubMemberProxy.setFather_uid(father_uid);
		clubMemberProxy.setFanyong(0f);
		clubMemberProxyMapper.insert(clubMemberProxy);
		ClubMemberPermission clubMemberPermission = new ClubMemberPermission();
		clubMemberPermission.setAdd_person(0);
		clubMemberPermission.setFrozen_line(0);
		clubMemberPermission.setFrozen_person(0);
		clubMemberPermission.setFrozen_person_gold(0);
		clubMemberPermission.setKick_club(0);
		clubMemberPermission.setAdd_money(0);
		clubMemberPermission.setReduce_money(0);
		clubMemberPermission.setSet_hehuoren(0);
		clubMemberPermission.setDel_hehuoren(0);
		clubMemberPermission.setKick_room(0);
		clubMemberPermission.setCreate_game_rule(0);
		clubMemberPermission.setSwitch_game_rule(0);
		clubMemberPermission.setLook_allsubs(0);
		clubMemberPermissionMapper.insert(clubMemberPermission);
		ClubMemberRole clubMemberRole = new ClubMemberRole();
		clubMemberRole.setClub_id(club_id);
		clubMemberRole.setUid(uid);
		clubMemberRole.setRole(ClubRoleId.NOMAL);
		clubMemberRole.setPermission_id(clubMemberPermission.getPermission_id());
		clubMemberRoleMapper.insert(clubMemberRole);
		remoteExtension.proxyAdd(new ProxyAdd(club_id, uid, father_uid));
		redisUtil.hset(RedisConsts.A_CLUBMEMBER+"_"+club_id,String.valueOf(uid), JSONObject.toJSONString(clubMember));
		
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		InviteInfo inviteInfo=new InviteInfo();
		inviteInfo.setClub_id(club_id);
		inviteInfo.setUid(father_uid);
		inviteInfo.setOther_uid(uid);
		inviteInfo.setNick(simpleUser.getNick());
		inviteInfo.setOther_nick(user.getNick());
		inviteInfo.setCreate_stamp(now);
		inviteInfo.setClub_name(club.getClub_name());
		queueUtil.putQueue(QueueConsts.QUEUE_INVITE_LINE, inviteInfo);
		//添加俱乐部邀请成员日志
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.INVITE_MEMBER);
		logClubEvent.setEvent_msg("用户:"+father_uid+"邀请用户:"+uid+"进入俱乐部!");
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public ClubGameRule loadHotClubGameRule(int rule_id) {
		ClubGameRule clubGameRule=null;
		String str=(String) redisUtil.get(RedisConsts.HOT_CLUBGAMERULE+"_"+rule_id);
		if(StringUtils.isBlank(str)) {
			clubGameRule=clubGameRuleMapper.getClubGameRule(rule_id);
			redisUtil.set(RedisConsts.HOT_CLUBGAMERULE+"_"+rule_id, JSONObject.toJSONString(clubGameRule),1*60);
		}else {
			clubGameRule=JSONObject.parseObject(str,ClubGameRule.class);
		}
		return clubGameRule;
	}
	public List<ClubGameRule> loadHotClubGameRules(int club_id) {
		List<ClubGameRule> list=new ArrayList<>();
		String str=(String) redisUtil.get(RedisConsts.HOT_CLUBGAMERULESBY_CLUBID+String.valueOf(club_id));
		if(StringUtils.isBlank(str)) {
			List<ClubGameRule> tmp=clubGameRuleMapper.loadClubGameRulesByClubId(club_id);
			list.addAll(tmp);
			redisUtil.set(RedisConsts.HOT_CLUBGAMERULESBY_CLUBID+String.valueOf(club_id),JSONObject.toJSONString(list), 1*60);
		}else {
			List<ClubGameRule> tmp=JSONObject.parseObject(str,new TypeReference<List<ClubGameRule>>() {});
			list.addAll(tmp);
		}
		return list;
	}
	
	public ClubMemberProxy getClubMemberProxy(int club_id, int uid) {
		ClubMemberProxy clubMemberProxy=(ClubMemberProxy) redisUtil.hget(RedisConsts.HOT_CLUBMEMBER_PROXY+"_"+club_id, String.valueOf(uid));
		if(clubMemberProxy==null) {
			clubMemberProxy=clubMemberProxyMapper.getClubMemberProxy(club_id,uid);
			if(clubMemberProxy!=null) {
				redisUtil.hset(RedisConsts.HOT_CLUBMEMBER_PROXY+"_"+club_id, String.valueOf(uid), clubMemberProxy, 1*60);
			}
		}
		return clubMemberProxy;
	}
	
	public ZsInfo getZsUidsByPage(int club_id, int uid, int page, int pageLength) {
		int total_size=clubMemberProxyMapper.loadZsUidsTotalCountByPage(club_id,uid);
		int start=page*pageLength;
		List<Integer> list=clubMemberProxyMapper.loadZsUidsByPage(club_id,uid,start,pageLength);
		ZsInfo zsInfo=new ZsInfo();
		zsInfo.getZsuids().addAll(list);
		zsInfo.setTotal_size(total_size);
		return zsInfo;
	}
	public ClubMemberRole getClubMemberRole(int club_id, int uid) {
		ClubMemberRole clubMemberRole=(ClubMemberRole) redisUtil.hget(RedisConsts.HOT_CLUBMEMBER_ROLE+"_"+club_id, String.valueOf(uid));
		if(clubMemberRole==null) {
			clubMemberRole=clubMemberRoleMapper.getClubMemberRole(club_id,uid);
			if(clubMemberRole!=null) {
				redisUtil.hset(RedisConsts.HOT_CLUBMEMBER_ROLE+"_"+club_id, String.valueOf(uid), clubMemberRole, 1*60);
			}
		}
		return clubMemberRole;
	}
	public ClubMemberPermission getClubMemberPermission(int club_id, int uid) {
		ClubMemberPermission clubMemberPermission=(ClubMemberPermission) redisUtil.hget(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid));
		if(clubMemberPermission==null) {
			clubMemberPermission=clubMemberPermissionMapper.getClubMemberPermission(club_id,uid);
			if(clubMemberPermission!=null) {
				redisUtil.hset(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid), clubMemberPermission, 1*60);
			}
		}
		return clubMemberPermission;
	}
	public ZsInfo getZsHehuorenByPage(int club_id, int uid, int page, int pageLength) {
		int total_size=clubMemberProxyMapper.loadZsHehuorenTotalCountByPage(club_id,uid);
		int start=page*pageLength;
		List<Integer> list=clubMemberProxyMapper.loadZsHehuorenByPage(club_id,uid,start,pageLength);
		ZsInfo zsInfo=new ZsInfo();
		zsInfo.getZsuids().addAll(list);
		zsInfo.setTotal_size(total_size);
		return zsInfo;
	}
	public long getZsTotalTea(int club_id, int uid) {
		long total=clubMemberMapper.selectClubZsMemberTotalTea(club_id,uid);
		return total;
	}
	public long getHehuorenTotalTea(int club_id, int father_uid, int hehuoren_uid, List<Integer> childs) {
		long total=clubMemberMapper.selectClubHehuorenMemberTotalTea(club_id,childs);
		return total;
	}
	public void setHehuoren(int club_id, int father_uid, int uid) {
		ClubMemberPermission clubMemberPermission = new ClubMemberPermission();
		clubMemberPermission.setAdd_money(1);
		clubMemberPermission.setReduce_money(1);
		clubMemberPermission.setAdd_person(1);
		clubMemberPermission.setSet_hehuoren(1);
		clubMemberPermission.setDel_hehuoren(1);
		clubMemberPermissionMapper.insert(clubMemberPermission);
		clubMemberRoleMapper.updateRoleAndPermission(club_id,uid,ClubRoleId.HEHUOREN,clubMemberPermission.getPermission_id());
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_ROLE+"_"+club_id, String.valueOf(uid));
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid));
		
		//添加俱乐部设置合伙人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.SET_HEHUOREN);
		logClubEvent.setEvent_msg("用户:"+father_uid+"为用户:"+uid+"设置合伙人!");
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void delHehuoren(int club_id, Integer father_uid, int uid) {
		clubMemberProxyMapper.updateFanyong(club_id, uid, 0);
		ClubMemberRole clubMemberRole = clubMemberRoleMapper.getClubMemberRole(club_id, uid);
		int permission_id=clubMemberRole.getPermission_id();
		clubMemberRoleMapper.updateRoleAndPermission(club_id,uid,ClubRoleId.NOMAL,null);
		clubMemberPermissionMapper.deleteById(permission_id);
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_ROLE+"_"+club_id, String.valueOf(uid));
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid));
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PROXY+"_"+club_id, String.valueOf(uid));
		
		//添加俱乐部取消合伙人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.DEL_HEHUOREN);
		logClubEvent.setEvent_msg("用户:"+father_uid+"为用户:"+uid+"取消合伙人!");
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void frozenPerson(int club_id, Integer father_uid, int uid) {
		frozenUtil.insert(club_id,uid);
		
		//添加俱乐部冻结个人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.FROZEN_PERSON);
		logClubEvent.setEvent_msg("用户:"+father_uid+"冻结用户:"+uid);
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void frozenLine(int club_id, Integer father_uid, List<Integer> uids) {
		frozenUtil.insertBatch(club_id,uids);	
		
		//添加俱乐部冻结个人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.FROZEN_LINE);
		logClubEvent.setEvent_msg("用户:"+father_uid+"冻结用户整条线:"+JSONObject.toJSONString(uids));
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(JSONObject.toJSONString(uids));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public boolean setHehuorenPower(int club_id, Integer father_uid, int uid, ReqPermission reqPermission) {
		//检查设置的权限是否是自己所拥有
		ClubMemberPermission myPermission=getClubMemberPermission(club_id, father_uid);
		if(reqPermission.getAdd_money()!=null&&reqPermission.getAdd_money()==1) {
			if(myPermission.getAdd_money()==null||myPermission.getAdd_money()==0) {
				return false;
			}
		}
		if(reqPermission.getAdd_person()!=null&&reqPermission.getAdd_person()==1) {
			if(myPermission.getAdd_person()==null||myPermission.getAdd_person()==0) {
				return false;
			}
		}
		if(reqPermission.getDel_hehuoren()!=null&&reqPermission.getDel_hehuoren()==1) {
			if(myPermission.getDel_hehuoren()==null||myPermission.getDel_hehuoren()==0) {
				return false;
			}
		}
		if(reqPermission.getFrozen_line()!=null&&reqPermission.getFrozen_line()==1) {
			if(myPermission.getFrozen_line()==null||myPermission.getFrozen_line()==0) {
				return false;
			}
		}
		if(reqPermission.getFrozen_person()!=null&&reqPermission.getFrozen_person()==1) {
			if(myPermission.getFrozen_person()==null||myPermission.getFrozen_person()==0) {
				return false;
			}
		}
		if(reqPermission.getFrozen_person_gold()!=null&&reqPermission.getFrozen_person_gold()==1) {
			if(myPermission.getFrozen_person_gold()==null||myPermission.getFrozen_person_gold()==0) {
				return false;
			}
		}
		if(reqPermission.getKick_club()!=null&&reqPermission.getKick_club()==1) {
			if(myPermission.getKick_club()==null||myPermission.getKick_club()==0) {
				return false;
			}
		}
		if(reqPermission.getKick_room()!=null&&reqPermission.getKick_room()==1) {
			if(myPermission.getKick_room()==null||myPermission.getKick_room()==0) {
				return false;
			}
		}
		if(reqPermission.getReduce_money()!=null&&reqPermission.getReduce_money()==1) {
			if(myPermission.getReduce_money()==null||myPermission.getReduce_money()==0) {
				return false;
			}
		}
		if(reqPermission.getSet_hehuoren()!=null&&reqPermission.getSet_hehuoren()==1) {
			if(myPermission.getSet_hehuoren()==null||myPermission.getSet_hehuoren()==0) {
				return false;
			}
		}
		if(reqPermission.getCreate_game_rule()!=null&&reqPermission.getCreate_game_rule()==1) {
			if(myPermission.getCreate_game_rule()==null||myPermission.getCreate_game_rule()==0) {
				return false;
			}
		}
		if(reqPermission.getSwitch_game_rule()!=null&&reqPermission.getSwitch_game_rule()==1) {
			if(myPermission.getSwitch_game_rule()==null||myPermission.getSwitch_game_rule()==0) {
				return false;
			}
		}
		if(reqPermission.getLook_allsubs()!=null&&reqPermission.getLook_allsubs()==1) {
			if(myPermission.getLook_allsubs()==null||myPermission.getLook_allsubs()==0) {
				return false;
			}
		}
		//其他权限在这里进行扩展
		
		ClubMemberPermission clubMemberPermission = clubMemberPermissionMapper.getClubMemberPermission(club_id, uid);
		clubMemberPermission.setAdd_money(reqPermission.getAdd_money()==1?1:0);
		clubMemberPermission.setAdd_person(reqPermission.getAdd_person()==1?1:0);
		clubMemberPermission.setDel_hehuoren(reqPermission.getDel_hehuoren()==1?1:0);
		clubMemberPermission.setFrozen_line(reqPermission.getFrozen_line()==1?1:0);
		clubMemberPermission.setFrozen_person(reqPermission.getFrozen_person()==1?1:0);
		clubMemberPermission.setFrozen_person_gold(reqPermission.getFrozen_person_gold()==1?1:0);
		clubMemberPermission.setKick_club(reqPermission.getKick_club()==1?1:0);
		clubMemberPermission.setKick_room(reqPermission.getKick_room()==1?1:0);
		clubMemberPermission.setReduce_money(reqPermission.getReduce_money()==1?1:0);
		clubMemberPermission.setSet_hehuoren(reqPermission.getSet_hehuoren()==1?1:0);
		clubMemberPermission.setCreate_game_rule(reqPermission.getCreate_game_rule()==1?1:0);
		clubMemberPermission.setSwitch_game_rule(reqPermission.getSwitch_game_rule()==1?1:0);
		clubMemberPermission.setLook_allsubs(reqPermission.getLook_allsubs()==1?1:0);
		clubMemberPermissionMapper.update(clubMemberPermission);
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid));
		return true;
	}
	public void setHehuorenFanyong(int club_id, int father_uid, int uid, float fanyong) {
		clubMemberProxyMapper.updateFanyong(club_id,uid,fanyong);
		redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PROXY+"_"+club_id, String.valueOf(uid));
		
		//添加俱乐部设置返佣个人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.SET_FANYONG);
		logClubEvent.setEvent_msg("用户:"+father_uid+"为用户:"+uid+"设置返佣:"+fanyong);
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void createGameRule(int uid,int club_id, int game_id, String pipei_key, String class_name, String config_str,int fzb_playernum) {
		ClubGame clubGame=clubGameMapper.getClubGame(club_id,game_id);
		if(clubGame==null) {
			clubGame=new ClubGame();
			clubGame.setClub_id(club_id);
			clubGame.setGame_id(game_id);
			clubGameMapper.insert(clubGame);
		}
		ClubGameRule clubGameRule =new ClubGameRule();
		clubGameRule.setClass_name(class_name);
		clubGameRule.setClub_id(club_id);
		clubGameRule.setConfig_str(config_str);
		clubGameRule.setGame_id(game_id);
		clubGameRule.setOpen(0);
		clubGameRule.setPipei_key(pipei_key);
		clubGameRule.setFzb_playernum(fzb_playernum);
		clubGameRuleMapper.insert(clubGameRule);
		redisUtil.del(RedisConsts.HOT_CLUB_GAMES+String.valueOf(club_id));
		redisUtil.del(RedisConsts.HOT_CLUBGAMERULE+"_"+clubGameRule.getRule_id());
		redisUtil.del(RedisConsts.HOT_CLUBGAMERULESBY_CLUBID+String.valueOf(club_id));
		redisUtil.del(RedisConsts.HOT_SELECT_GAMERULES+"_"+club_id);
		
		//添加俱乐部创建游戏玩法日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.CREATE_GAME_RULE);
		logClubEvent.setEvent_msg("用户:"+uid+"创建了玩法");
		logClubEvent.setEvent_creater(uid);
		logClubEvent.setEvent_joiner("");
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public ResSelectGameRules selectGameRules(Integer uid, int club_id, String class_name, int page, int pageLength) {
		ResSelectGameRules resSelectGameRules= (ResSelectGameRules) redisUtil.hget(RedisConsts.HOT_SELECT_GAMERULES+"_"+club_id,page+"_"+pageLength+"_"+class_name);
		if(resSelectGameRules==null) {
			int total_size=clubGameRuleMapper.loadGameRuleTotalCountByPage(club_id,class_name);
			int start=page*pageLength;
			List<ClubGameRule> list=clubGameRuleMapper.loadGameRulesByPage(club_id,class_name,start,pageLength);
			PageBean<ClubGameRule> pageBean=new PageBean<>(total_size, pageLength, page, list);
			resSelectGameRules = new ResSelectGameRules();
			resSelectGameRules.setPageBean(pageBean);
			redisUtil.hset(RedisConsts.HOT_SELECT_GAMERULES+"_"+club_id,page+"_"+pageLength+"_"+class_name,resSelectGameRules,1*60);
		}
		return resSelectGameRules;
	}
	public void switchGameRule(Integer uid, int club_id, int rule_id, int open) {
		clubGameRuleMapper.updateSwitch(rule_id,open);
		redisUtil.del(RedisConsts.HOT_CLUBGAMERULE+"_"+rule_id);
		redisUtil.del(RedisConsts.HOT_CLUBGAMERULESBY_CLUBID+String.valueOf(club_id));
		redisUtil.del(RedisConsts.HOT_SELECT_GAMERULES+"_"+club_id);
		
		//添加俱乐部开关游戏玩法日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.OPEN_GAME_RULE);
		logClubEvent.setEvent_msg("用户:"+uid+"开关了玩法");
		logClubEvent.setEvent_creater(uid);
		logClubEvent.setEvent_joiner("");
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void editNotice(int club_id, int uid,String notice) {
		clubMapper.updateNotice(club_id,notice);
		//添加俱乐部开关游戏玩法日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.EDIT_NOTICE);
		logClubEvent.setEvent_msg("用户:"+uid+"修改了公告");
		logClubEvent.setEvent_creater(uid);
		logClubEvent.setEvent_joiner("");
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);
	}
	public void cancleFrozenPerson(int club_id, Integer father_uid, int uid) {
		frozenUtil.delete(club_id,uid);
		
		//添加俱乐部冻结个人日志
		Date now=new Date();
		String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
		LogClubEvent logClubEvent = new LogClubEvent();
		logClubEvent.setEvent_type(LogClubEventType.CANCLE_FROZEN_PERSON);
		logClubEvent.setEvent_msg("用户:"+father_uid+"解冻用户:"+uid);
		logClubEvent.setEvent_creater(father_uid);
		logClubEvent.setEvent_joiner(String.valueOf(uid));
		logClubEvent.setDay_str(day_str);
		logClubEvent.setCreate_stamp(now);
		queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);		
	}
	
	public void kickClub(int club_id, Integer op_uid, int uid) {
		ClubMemberProxy clubMemberProxy = getClubMemberProxy(club_id, uid);
		int father_uid=clubMemberProxy.getFather_uid();
		ClubMember clubMember = getClubMember(uid, club_id);
		long money=clubMember.getTea()+clubMember.getSafety()+clubMember.getFrozen();
		ClubMemberRole clubMemberRole=getClubMemberRole(club_id, uid);
		//开始记录金币变化日志
		List<LogGoldChange> logGoldChanges = new ArrayList<>();
		//开始记录交易日志
		List<LogTrade> logTrades = new ArrayList<>();
		boolean ok=false;
		if(money>0) {
			//需要自动将其分数加入到直属上级身上
			Resp resp=tradeService.kickClubAddTeaToFather(club_id,uid,father_uid,logGoldChanges,logTrades);
			if(Code.OK.equals(resp.getCode())) {
				ok=true;
			}
		}else {
			ok=true;
		}
		if(ok) {
			//开始重新绑定关系
			clubMemberProxyMapper.rebind(club_id,father_uid,uid);
			//开始清理成员信息
			clubMemberMapper.delete(club_id,uid);
			clubMemberPermissionMapper.delete(clubMemberRole.getPermission_id());
			clubMemberProxyMapper.delete(club_id,uid);
			clubMemberRoleMapper.delete(club_id,uid);
			clubMemberWaterpakMapper.delete(club_id,uid);
			queueUtil.putQueueBatch(QueueConsts.QUEUE_LOG_GOLD_CHANGE, logGoldChanges);//推送金币变化日志
			queueUtil.putQueueBatch(QueueConsts.QUEUE_LOG_TRADE, logTrades);//推送交易日志
			//开始通知账户变化
			clubMemberUtil.notifyClubMemberChange(club_id,father_uid);
			notifyUtil.notifyData(Message.build(GATECMD.NOTIFY_BE_KICKOUT_CLUB, Resp.build(Code.OK, "被T出茶馆",PackageUtil.packageNotifyBeKickoutClub(club_id))),uid);

			remoteExtension.proxyRebind(new ProxyRebind(club_id,father_uid,uid));
			redisUtil.hdel(RedisConsts.A_CLUBMEMBER+"_"+club_id,String.valueOf(uid));
			redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_ROLE+"_"+club_id, String.valueOf(uid));
			redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PERMISSION+"_"+club_id, String.valueOf(uid));
			redisUtil.hdel(RedisConsts.HOT_CLUBMEMBER_PROXY+"_"+club_id, String.valueOf(uid));
			
			//添加俱乐部冻结个人日志
			Date now=new Date();
			String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
			LogClubEvent logClubEvent = new LogClubEvent();
			logClubEvent.setEvent_type(LogClubEventType.KICKOUT_PERSON);
			logClubEvent.setEvent_msg("用户:"+op_uid+"T出用户:"+uid);
			logClubEvent.setEvent_creater(op_uid);
			logClubEvent.setEvent_joiner(String.valueOf(uid));
			logClubEvent.setDay_str(day_str);
			logClubEvent.setCreate_stamp(now);
			queueUtil.putQueue(QueueConsts.QUEUE_LOG_CLUB_EVENT, logClubEvent);		
		}else {
			throw new RuntimeException("T出茶馆失败");
		}
	}
	public Float getSubMaxFanyong(int club_id, int uid) {
		return clubMemberProxyMapper.getSubMaxFanyong(club_id,uid);
	}
}
